package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

type User2 struct {
	ID   int8   `json:"id" form:"id"`
	Name string `json:"name" form:"name"`
	Pass int    `json:"password" form:"pass"`
}

func main() {
	dsn := "root:170815@tcp(10.1.3.10:3306)/lianxi?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
			NoLowerCase:   true,
		},
	})
	if err != nil {
		panic(err)
	}

	//创建
	/*db.AutoMigrate(&User2{})
	u := User2{Name: "王莹", Pass: 12345, Age: 14}
	db.Create(&u)
	u := User2{Name: "xiaoying", Pass: 12345, Age: 14}
	db.Create(&u)*/
	/*user2 := new(User2)
	db.First(user2)
	fmt.Println(user2)*/
	//user2 := new(User2)
	//db.Where("name = ?", "xiaoying").First(&user2)
	//fmt.Println(user2)
	user2 := new(User2)
	r := gin.Default()
	r.POST("/user-sign", func(c *gin.Context) {
		user := &User2{}
		//db.Where("name=? AND pass=?", user.Name, user.Pass).First(&user2)

		if err := c.ShouldBind(&user); err == nil {
			db.Where("name=? AND pass=?", user.Name, user.Pass).First(&user2)
			if user2.Name == user.Name {
				if user2.Pass == user.Pass {
					c.JSON(200, gin.H{
						"message": "登陆成功",
					})
				} else {
					c.JSON(200, gin.H{
						"message": "登陆失败",
					})
				}

			} else {
				c.JSON(200, gin.H{
					"message": "登陆失败",
				})
			}
		} else {
			c.JSON(200, gin.H{
				"err": err.Error(),
			})
			/*if user2.ID == 0 {
					c.JSON(200, gin.H{
						"message": "登陆成功",
					})
				} else {
					c.JSON(200, gin.H{
						"message": "登陆成功",
					})
				}

			} else {
				c.JSON(200, gin.H{
					"err": err.Error(),
				})*/
		}
		//fmt.Println(user)
		//user2 := new(User2)
		//db.Where("name=? AND pass=? AND id=?", user.Name, user.Pass, user.ID).First(&user2)
		//fmt.Println(user2)
		/*if user2.ID == 0 {
			fmt.Println("登录失败")
		} else {
			fmt.Println("登陆成功")
		}*/

	})
	r.Run()

}
